package com.example.pc.listview05;

import android.app.Fragment;
import android.app.FragmentTransaction;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import static android.R.id.list;
import static com.example.pc.listview05.R.id.textView1;
import static com.example.pc.listview05.R.layout.item_1;


/*
                           _ooOoo_
                          o8888888o
                          88" . "88
                          (| -_- |)
                          O\  =  /O
                       ____/`---'\____
                     .'  \\|     |//  `.
                    /  \\|||  :  |||//  \
                   /  _||||| -:- |||||-  \
                   |   | \\\  -  /// |   |
                   | \_|  ''\---/''  |   |
                   \  .-\__  `-`  ___/-. /
                 ___`. .'  /--.--\  `. . __
              ."" '<  `.___\_<|>_/___.'  >'"".
             | | :  `- \`.;`\ _ /`;.`/ - ` : | |
             \  \ `-.   \_ __\ /__ _/   .-` /  /
        ======`-.____`-.___\_____/___.-`____.-'======
                           `=---='
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                 佛祖保佑       永无BUG
        */

public class MainActivity extends AppCompatActivity {


    private ListView listView;
    private Button button2;      //refresh button
    private Button button3;     // add button
    private Button button4;     // dele button
    private List<String> data;
    private int x = 30;
    private MyAdapter adapter;
    private Context context = this;
    private TextView textview1;

    private void initUI() {
        listView = (ListView) findViewById(R.id.list_view);
        button2 = (Button) findViewById(R.id.title_button);
        button3 = (Button) findViewById(R.id.add_button);
        button4 = (Button) findViewById(R.id.dele_button);
    }

    private void initData() {
        data = new ArrayList<String>();
        for (int i = 0; i < x; i++) {
            data.add(String.valueOf(i));
//           data.add("第"+i+"项");
        }
        adapter = new MyAdapter(MainActivity.this, data);//new一个适配器对象
        listView.setAdapter(adapter);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        initUI();
        initData();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                Bundle b = new Bundle();
                b.putString("key", "第" + adapter.getItem_1().get(position) + "项"); //第一个参数用来传值，第二个参数是string型
                intent.putExtras(b);
                startActivity(intent);
            }
        });
//       listView.setSelection(adapter.getCount()-1);//listview自动下滑到底部
//       listView.setSelection(listView.getBottom());

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                adapter.notifyDataSetChanged();
                listView.setSelection(listView.getBottom());
            }


        });

        button3.setOnClickListener(new View.OnClickListener() {

            @Override

            public void onClick(View v) {

                data.add(String.valueOf(x));
                listView.setSelection(listView.getBottom());
                x = x + 1;
            }
        });

        button4.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                adapter.deleteItem();
                listView.setSelection(listView.getBottom());
            }
        });

        listView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                adapter.deleteItemInListView(position);//获取当前item位置并删除
                Toast.makeText(MainActivity.this,"you delete the item",Toast.LENGTH_SHORT).show();
                return true;
            }


        });




    }
}
